package com.mk.web;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mk.web.model.Flower;

/**
 * mybatis 集成 tomcat jndi
 * Created by WangChen on 2018/11/27 0:12.
 */
@WebServlet("/mybatisTomcatJndi")
public class MybatisUsingTomcatJndiServlet extends HttpServlet{

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-tomcat-jndi.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = factory.openSession();
        List<Flower> flowerList = sqlSession.selectList("a.b.c.selectByName", "花2");

        for (Flower flower : flowerList){
            System.out.println("MybatisUsingTomcatJndiServlet=============" + flower);
        }

        sqlSession.close();
    }
}
